home *** CD-ROM | disk | FTP | other *** search
- Public Class Form1
- Inherits System.Windows.Forms.Form
-
- #Region " Windows Form Designer generated code "
-
- Public Sub New()
- MyBase.New()
-
- 'This call is required by the Windows Form Designer.
- InitializeComponent()
-
- 'Add any initialization after the InitializeComponent() call
-
- End Sub
-
- 'Form overrides dispose to clean up the component list.
- Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
- If disposing Then
- If Not (components Is Nothing) Then
- components.Dispose()
- End If
- End If
- MyBase.Dispose(disposing)
- End Sub
- Friend WithEvents Label1 As System.Windows.Forms.Label
- Friend WithEvents Label2 As System.Windows.Forms.Label
- Friend WithEvents btnStandard As System.Windows.Forms.Button
- Friend WithEvents txtObjects As System.Windows.Forms.TextBox
- Friend WithEvents txtIterations As System.Windows.Forms.TextBox
- Friend WithEvents btnPooled As System.Windows.Forms.Button
- Friend WithEvents lblStandard As System.Windows.Forms.Label
- Friend WithEvents lblPooled As System.Windows.Forms.Label
-
- 'Required by the Windows Form Designer
- Private components As System.ComponentModel.Container
-
- 'NOTE: The following procedure is required by the Windows Form Designer
- 'It can be modified using the Windows Form Designer.
- 'Do not modify it using the code editor.
- <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
- Me.txtObjects = New System.Windows.Forms.TextBox()
- Me.txtIterations = New System.Windows.Forms.TextBox()
- Me.Label1 = New System.Windows.Forms.Label()
- Me.Label2 = New System.Windows.Forms.Label()
- Me.lblStandard = New System.Windows.Forms.Label()
- Me.btnStandard = New System.Windows.Forms.Button()
- Me.btnPooled = New System.Windows.Forms.Button()
- Me.lblPooled = New System.Windows.Forms.Label()
- Me.SuspendLayout()
- '
- 'txtObjects
- '
- Me.txtObjects.Location = New System.Drawing.Point(208, 16)
- Me.txtObjects.Name = "txtObjects"
- Me.txtObjects.Size = New System.Drawing.Size(72, 26)
- Me.txtObjects.TabIndex = 2
- Me.txtObjects.Text = "10"
- '
- 'txtIterations
- '
- Me.txtIterations.Location = New System.Drawing.Point(208, 64)
- Me.txtIterations.Name = "txtIterations"
- Me.txtIterations.Size = New System.Drawing.Size(72, 26)
- Me.txtIterations.TabIndex = 2
- Me.txtIterations.Text = "100"
- '
- 'Label1
- '
- Me.Label1.Font = New System.Drawing.Font("Microsoft Sans Serif", 12!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
- Me.Label1.Location = New System.Drawing.Point(32, 16)
- Me.Label1.Name = "Label1"
- Me.Label1.Size = New System.Drawing.Size(144, 24)
- Me.Label1.TabIndex = 1
- Me.Label1.Text = "max # of objects"
- Me.Label1.TextAlign = System.Drawing.ContentAlignment.MiddleRight
- '
- 'Label2
- '
- Me.Label2.Font = New System.Drawing.Font("Microsoft Sans Serif", 12!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
- Me.Label2.Location = New System.Drawing.Point(32, 64)
- Me.Label2.Name = "Label2"
- Me.Label2.Size = New System.Drawing.Size(144, 24)
- Me.Label2.TabIndex = 1
- Me.Label2.Text = "# of iterations"
- Me.Label2.TextAlign = System.Drawing.ContentAlignment.MiddleRight
- '
- 'lblStandard
- '
- Me.lblStandard.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
- Me.lblStandard.Cursor = System.Windows.Forms.Cursors.No
- Me.lblStandard.Location = New System.Drawing.Point(208, 120)
- Me.lblStandard.Name = "lblStandard"
- Me.lblStandard.Size = New System.Drawing.Size(200, 40)
- Me.lblStandard.TabIndex = 3
- '
- 'btnStandard
- '
- Me.btnStandard.Location = New System.Drawing.Point(32, 120)
- Me.btnStandard.Name = "btnStandard"
- Me.btnStandard.Size = New System.Drawing.Size(152, 40)
- Me.btnStandard.TabIndex = 0
- Me.btnStandard.Text = "standard GCs"
- '
- 'btnPooled
- '
- Me.btnPooled.Location = New System.Drawing.Point(32, 176)
- Me.btnPooled.Name = "btnPooled"
- Me.btnPooled.Size = New System.Drawing.Size(152, 40)
- Me.btnPooled.TabIndex = 0
- Me.btnPooled.Text = "Pooled objects"
- '
- 'lblPooled
- '
- Me.lblPooled.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
- Me.lblPooled.Cursor = System.Windows.Forms.Cursors.No
- Me.lblPooled.Location = New System.Drawing.Point(208, 176)
- Me.lblPooled.Name = "lblPooled"
- Me.lblPooled.Size = New System.Drawing.Size(200, 40)
- Me.lblPooled.TabIndex = 3
- '
- 'Form1
- '
- Me.AutoScaleBaseSize = New System.Drawing.Size(8, 19)
- Me.ClientSize = New System.Drawing.Size(488, 261)
- Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.lblPooled, Me.lblStandard, Me.btnPooled, Me.Label2, Me.txtIterations, Me.txtObjects, Me.Label1, Me.btnStandard})
- Me.Font = New System.Drawing.Font("Microsoft Sans Serif", 12!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
- Me.Name = "Form1"
- Me.Text = "Object Pool Demo"
- Me.ResumeLayout(False)
-
- End Sub
-
- #End Region
-
- Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStandard.Click
- Dim i As Integer, j As Integer
- Dim rand As New Random()
- Dim objCount As Integer
- Dim start As Date
-
- ' max number of objects
- Dim objMax As Integer = CInt(txtObjects.Text)
-
- ' create an array that holds these objects
- Dim arrObj(objMax) As Object
-
- start = Now
-
- ' repeat for the number of iterations specified
- For i = 1 To CInt(txtIterations.Text)
-
- ' get the next number of objects that should go into the array
- Dim objNextCount As Integer = rand.Next(0, objMax)
-
- If objNextCount > objCount Then
- ' we must create a few objects
- For j = objCount + 1 To objNextCount
- arrObj(j) = New RandomArray()
- Next
- ElseIf objNextCount < objCount Then
- ' else we must release one or more objects
- For j = objNextCount + 1 To objCount
- arrObj(j) = Nothing
- Next
- End If
- objCount = objNextCount
- Next
-
- ' clear the array and force a garbage collection
- arrObj = Nothing
- GC.Collect()
-
- ' display total time
- lblStandard.Text = Now.Subtract(start).ToString
-
- End Sub
-
- Private Sub btnPooled_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnPooled.Click
- Dim i As Integer, j As Integer
- Dim rand As New Random()
- Dim objCount As Integer
- Dim start As Date
-
- ' max number of objects
- Dim objMax As Integer = CInt(txtObjects.Text)
-
- ' create an array that holds these objects
- Dim arrObj(objMax) As Object
-
- start = Now
-
- ' create the poolmanager
- Dim PoolMan As New PoolManager()
-
- ' repeat for the number of iterations specified
- For i = 1 To CInt(txtIterations.Text)
-
- ' get the next number of objects that should go into the array
- Dim objNextCount As Integer = rand.Next(0, objMax)
-
- If objNextCount > objCount Then
- ' we must create a few objects
- For j = objCount + 1 To objNextCount
- arrObj(j) = PoolMan.NewRandomArray
- Next
- ElseIf objNextCount < objCount Then
- ' else we must release one or more objects
- For j = objNextCount + 1 To objCount
- arrObj(j) = Nothing
- Next
- End If
- ' remember current objCount
- objCount = objNextCount
- Next
-
- ' clear the array and force a garbage collection
- arrObj = Nothing
-
- PoolMan.Dispose()
- PoolMan = Nothing
- GC.Collect()
-
- ' display total time
- lblPooled.Text = Now.Subtract(start).ToString
-
- End Sub
- End Class
-